package com.vincce.sport.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.vincce.sport.entity.Team;
import com.vincce.sport.entity.User;
import com.vincce.sport.mapper.TeamMapper;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.Constant;
import com.vincce.sport.core.utils.ConvertUtil;
import com.vincce.sport.entity.Page;
import com.vincce.sport.mapper.UserMapper;

@Controller
@RequestMapping("/userManage")
public class UserController extends BaseController{

	@Resource
	private UserMapper userMapper;
    @Resource
    private TeamMapper teamMapper;

    @RequestMapping("/userList")
    public String userList(Model model, User user, Page<User> page) throws IOException{
        Map<String, Object> filter = new HashMap<String, Object>();
        if(user.getUserName() != null && !user.getUserName().equals("")){
            filter.put("userName", user.getUserName());
        }
        if(user.getStatus() != null && !user.getStatus().equals("")){
            filter.put("status", user.getStatus());
        }
        page.setFilter(filter);
        
        page.setResult(userMapper.queryUserList(page));
        page.setTotal(userMapper.queryUserCount(page.getFilter()));
        model.addAttribute("page",page);                
        return "backstage/memberList";
    }

    @RequestMapping("/userAddOrUpdate")
    @ResponseBody
    public String addOrUpdateUser(Model model, User user, HttpServletRequest request) throws IOException{
    	Long count = 0l;
        //密码md5加密
    	String pwd = user.getPassword();
        if(user.getPassword()!=null && !"".equals(user.getPassword())){
        	try {
        		user.setPassword(ConvertUtil.encryptMD5(user.getPassword()));
			} catch (Exception e) {
				e.printStackTrace();
			}
        }                
        if(CheckUtil.isEmpty(user.getId())){
        	//创建 人和创建时间
        	user.setCreateUser(getCurrentUser().getId().intValue());
        	user.setCreateTime(new Date());
        	user.setUpdateTime(new Date());
            count = userMapper.insertUser(user);
            BbsController.testRegister(user.getUserName(), pwd, user.getEmail());
        } else{
        	//更新人和更新时间
            user.setUpdateUser(getCurrentUser().getId().intValue());
            user.setUpdateTime(new Date());
        	count = userMapper.updateUser(user);
        }                 
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("suc", count > 0 ? true : false);
        return JSON.toJSONString(result);
    }

    @RequestMapping("/userDelete")
    @ResponseBody
    public String deleteUser(Model model,User user, Long id, HttpServletRequest request) throws IOException{
        boolean flag=false;

        userMapper.deleteById(id);
               
        flag = true;
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("obj", flag);
        return JSON.toJSONString(result);
    }

    /**
	 * 檢查用户是否已存在
	 * @param userName
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/checkUserName")
	@ResponseBody
	public String checkUserName(String userName,Long id) throws Exception
	{
		Integer fg = 1;
		if(checkUserNameList(userName,id)){
			fg=0;
		}
		return Boolean.toString(fg==0);
	}
    public Boolean checkUserNameList(String userName,Long id){
    	User user=new User();
        if(userName != null && !userName.equals("")){
            user.setUserName(userName);
        }
        if(id!= null && !id.equals("")){
            user.setId(id);
        }
        
        Integer fg = 0;
        List<User> userList = userMapper.findUserByOne(user);
        if(userList!=null && userList.size()>0){
        	fg = 1;
        }
        return fg==0;
    }
    
}
